package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.watson.R;
import com.watson.utils.SPUtils;
import com.watson.utils.StringUtils;

import java.util.ArrayList;
import java.util.Map;


public class CodeAdapter extends RecyclerView.Adapter<CodeAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;
    private ArrayList<Map> dataList = new ArrayList<>();
    private String taskrecipientid = "";
    private String billstatus = "";

    public CodeAdapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setDataList(ArrayList<Map> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setTask(String taskrecipientid, String billstatus) {
        this.taskrecipientid = taskrecipientid;
        this.billstatus = billstatus;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_code, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.tvname.setText(StringUtils.isEmpty(mList.get(position).get("name") + "") ? "" : mList.get(position).get("name") + "");
            holder.tvsly.setText(StringUtils.isEmpty(mList.get(position).get("erpno") + "") ? "" : mList.get(position).get("erpno") + "");
            holder.tvPin.setText(StringUtils.isEmpty(mList.get(position).get("brand") + "") ? "" : mList.get(position).get("brand") + "");
            holder.tvstd.setText(StringUtils.isEmpty(mList.get(position).get("std") + "") ? "" : mList.get(position).get("std") + "");
            holder.tverpno.setText(StringUtils.isEmpty(mList.get(position).get("motorid") + "") ? "" : mList.get(position).get("motorid") + "");
            holder.tvcar.setText(StringUtils.isEmpty(mList.get(position).get("car") + "") ? "" : mList.get(position).get("car") + "");

            if (taskrecipientid.trim().equals(SPUtils.get(mContext, "U_Id", "") + "") && (billstatus.trim().equals("打包中") || billstatus.trim().equals("验单中") || billstatus.trim().equals("入库中"))) {
                holder.tvdel.setVisibility(View.VISIBLE);
            } else {
                holder.tvdel.setVisibility(View.GONE);
            }
            if (dataList.size() > 0) {
                String erpno = StringUtils.isEmpty(mList.get(position).get("erpno") + "") ? "" : mList.get(position).get("erpno") + "";
                for (int i = 0; i < dataList.size(); i++) {
                    ArrayList<Map> codesupplierList = new ArrayList<>();
                    if (dataList.get(i).containsKey("codesupplierList")) {
                        codesupplierList = (ArrayList<Map>) JSON.parseArray(dataList.get(i).get("codesupplierList") + "", Map.class);

                        if (codesupplierList.size() > 0) {
                            for (int j = 0; j < codesupplierList.size(); j++) {
                                String erpno2 = codesupplierList.get(j).get("erpno") + "";
                                if (erpno.trim().equals(erpno2)) {
                                    holder.tvname.setText(dataList.get(i).get("name") + "");
                                    holder.tvcar.setText(codesupplierList.get(j).get("car") + "");
                                }
                            }
                        }
                    }
                }
            }
            Boolean sw_jixing = (Boolean) SPUtils.get(mContext, "sw_jixing", true);
            if (sw_jixing) {
                holder.lyChe.setVisibility(View.VISIBLE);
            } else {
                holder.lyChe.setVisibility(View.GONE);
            }

            holder.tvdel.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvstd, tverpno, tvsly, tvname, tvcar, tvPin, tvdel;
        private LinearLayout lyChe;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvstd = itemView.findViewById(R.id.tvstd);
            tverpno = itemView.findViewById(R.id.tverpno);
            tvsly = itemView.findViewById(R.id.tvsly);
            tvname = itemView.findViewById(R.id.tvname);
            tvcar = itemView.findViewById(R.id.tvcar);
            tvPin = itemView.findViewById(R.id.tvPin);
            tvdel = itemView.findViewById(R.id.tvdel);
            lyChe = itemView.findViewById(R.id.lyChe);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, ArrayList<Map> mList);
    }
}
